package com.wing.wcabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.wcabinet.dao.WauthorityMemberDao;
import com.wing.wcabinet.model.entity.WauthorityMember;
import com.wing.wcabinet.model.request.WauthorityMemberForm;
import com.wing.wcabinet.model.response.WauthorityMemberVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 权限组成员表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class WauthorityMemberServiceImpl extends ServiceImpl<WauthorityMemberDao, WauthorityMember> implements WauthorityMemberService {



    @Transactional
    @Override
    public int add(WauthorityMemberForm authorityMemberForm) {
        //校验
        WauthorityMember authorityMember =new WauthorityMember();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(WauthorityMember.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(authorityMemberForm, authorityMember, copyOptions);
        authorityMember.setCreator(JwtUtils.getLoginUserId());
        authorityMember.setCreatorName(JwtUtils.getLoginUsername());
        int result = baseMapper.insert(authorityMember);
        return result;
    }

    @Override
    public int addBatch(List<WauthorityMemberForm> authorityMemberFormList) {
        int result = 0;
        for (WauthorityMemberForm authorityMemberForm:authorityMemberFormList){
            WauthorityMember authorityMember =new WauthorityMember();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WauthorityMember.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(authorityMemberForm, authorityMember, copyOptions);
            baseMapper.insert(authorityMember);
            result++;
        }
        return result;
    }

    @Override
    public List<WauthorityMemberVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<WauthorityMemberVO> pageList4app(Page page, Map<String, Object> params) {
        List<WauthorityMemberVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WauthorityMemberVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Transactional
    @Override
    public int deleteAuthorityMemberById(Long id) {
        WauthorityMember authorityMember = baseMapper.selectById(id);
        if(authorityMember == null){
            throw new BusinessException("删除失败");
        }
        int result = baseMapper.deleteById(id);
        return result;
    }

    @Transactional
    @Override
    public int batchDeleteAuthorityMemberById(Long[] ids) {
        if(ids!=null){
            return baseMapper.deleteBatchIds(Arrays.asList(ids));
        }
        return 0;
    }

}
